package com.haizhi.crm.sys.auth.service.impl;

import com.haizhi.crm.common.constant.SysStatus;
import com.haizhi.crm.common.exception.UnexpectedStatusException;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;
import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.common.util.Md5SaltUtils;
import com.haizhi.crm.sys.auth.dao.InstitutionDao;
import com.haizhi.crm.sys.auth.model.InstitutionDo;
import com.haizhi.crm.sys.auth.model.InstitutionQo;
import com.haizhi.crm.sys.auth.service.InstitutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by yangyijun on 2018/5/28.
 */
@Service
@Transactional
public class InstitutionServiceImpl implements InstitutionService {

    private static final GLog LOG = LogFactory.getLogger(InstitutionServiceImpl.class);

    @Autowired
    private InstitutionDao institutionDao;

    @Override
    public Response save(InstitutionQo institutionQo) {
        try {
            String signature = institutionQo.getSignature();
            institutionQo.setSignature(null);
            String localSignature = Md5SaltUtils.getEncrypted(institutionQo.toString());
            if (localSignature.equals(signature)) {
                InstitutionDo institutionDo = new InstitutionDo();
                institutionDo.setId(institutionQo.getId());
                institutionDo.setName(institutionQo.getName());
                institutionDo.setAddress(institutionQo.getAddress());
                institutionDo.setDistrictCode(institutionQo.getDistrictCode());
                institutionDo.setInstitutioncol(institutionQo.getInstitutioncol());
                institutionDo.setLevelId(institutionQo.getLevelId());
                institutionDo.setPbocPayOrg(institutionQo.getPbocPayOrg());
                institutionDo.setPhone(institutionQo.getPhone());
                institutionDo.setProvinceCode(institutionQo.getProvinceCode());
                institutionDo.setShortName(institutionQo.getShortName());
                institutionDo.setSuperiorId(institutionQo.getSuperiorId());
                institutionDo.setTypeId(institutionQo.getTypeId());
                institutionDo.setZipCode(institutionQo.getZipCode());
                institutionDo.setEnabledFlag(institutionQo.getEnabledFlag());
                institutionDo.setUpdatedDt(new Date());
                InstitutionDo i = institutionDao.findOne(institutionQo.getId());
                if (null == i) {
                    institutionDo.setCreatedDt(new Date());
                }
                institutionDao.save(institutionDo);
                return Response.success();
            } else {
                throw new UnexpectedStatusException(SysStatus.SYNC_INSTITUTION_ERROR);
            }
        } catch (Exception e) {
            LOG.error("sync institution info error", e);
            throw new UnexpectedStatusException(SysStatus.SYNC_INSTITUTION_ERROR, e);
        }
    }
}
